import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ThisIllusion on 2016/4/6.
 */
public class HttpTest {

    public static void main(String[] args) throws Exception {
        CookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        doPost(httpclient);
    }

    private static void doPost(CloseableHttpClient httpclient) throws IOException {
        HttpPost httpPost = new HttpPost("http://portal.yihaodian.com.cn/login.action");
        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("systemId", "20"));
        nvps.add(new BasicNameValuePair("backOperatorDto.nextNoLogin", "1"));
        nvps.add(new BasicNameValuePair("redirectURL", "http://portal.yihaodian.com.cn/portalToOtherSystem.action?systemId=3"));
        nvps.add(new BasicNameValuePair("backOperatorDto.userName", "wangziqiao"));
        nvps.add(new BasicNameValuePair("backOperatorDto.password", "Wzq12347"));
        nvps.add(new BasicNameValuePair("backOperatorDto.tokenId", ""));
        nvps.add(new BasicNameValuePair("radio", "on"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response2 = httpclient.execute(httpPost);

        try {
            System.out.println(response2.getStatusLine());
            HttpEntity entity2 = response2.getEntity();
            Document dom = Jsoup.parse(EntityUtils.toString(entity2));
            Elements iframes = dom.select("iframe");
            String url1 = iframes.get(0).attr("src");
            System.out.println(url1);
            String url2 = iframes.get(1).attr("src");
            System.out.println(url2);
            doStep2(httpclient,url1);
            EntityUtils.consume(entity2);
        } finally {
            response2.close();
        }
    }

    private static void doStep2(CloseableHttpClient httpclient,String url1) throws IOException {
        HttpGet httpGet = new HttpGet(url1);
        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        try {
            System.out.println(response1.getStatusLine());
            response1.getHeaders("Set-Cookie");
            HttpEntity entity1 = response1.getEntity();
            EntityUtils.consume(entity1);
            doStep3(httpclient);
        } finally {
            response1.close();
        }
    }

    private static void doStep3(CloseableHttpClient httpclient) throws IOException {
        HttpGet httpGet = new HttpGet("http://backend.yihaodian.com/index.action");
        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        try {
            System.out.println(response1.getStatusLine());
            HttpEntity entity1 = response1.getEntity();
            System.out.println(EntityUtils.toString(entity1));
            EntityUtils.consume(entity1);
        } finally {
            response1.close();
        }
    }
}
